package com.excuse.excusebuilder.presenter;

import android.content.Context;
import android.content.Intent;

import com.excuse.excusebuilder.base.IBaseList;
import com.excuse.excusebuilder.base.ToastQueryListener;
import com.excuse.excusebuilder.bean.Option;
import com.excuse.excusebuilder.bean.Result;
import com.excuse.excusebuilder.view.activity.AddResultActivity;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by 鹏君 on 2017/4/18.
 */

public class ExcuseActivityPresenter {
    private Context context;
    private IView activity;

    public ExcuseActivityPresenter(Context context, IView activity) {
        this.context = context;
        this.activity = activity;
    }

    public void getExcuseText(Option option) {
        BmobQuery<Result> query = new BmobQuery<>();
        query.addWhereEqualTo("option", new BmobPointer(option));
        query.findObjects(new ToastQueryListener<Result>(context, activity) {
            @Override
            public void onSuccess(List<Result> list) {
                activity.setContentList(list);
            }
        });

    }

    public void enterAddResultActivity(Option option) {
        Intent intent = new Intent(context, AddResultActivity.class);
        intent.putExtra(AddResultActivity.PARAM_OPTION, option);
        context.startActivity(intent);
    }

    public void deleteResult(Result result) {
        Result delete=new Result();
        delete.delete(result.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e != null) {
                    e.printStackTrace();
                }
                activity.deleteResult();
            }
        });

    }


    public interface IView extends IBaseList {


        void setContentList(List<Result> list);

        void deleteResult();

    }
}
